home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / socket.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  34.0 KB  |  1,345 lines  |  [TEXT/ALFA]

  1. # Commands tested in this file: socket.
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # Running socket tests with a remote server:
  13. # ------------------------------------------
  14. # Some tests in socket.test depend on the existence of a remote server to
  15. # which they connect. The remote server must be an instance of tcltest and it
  16. # must run the script found in the file "remote.tcl" in this directory. You
  17. # can start the remote server on any machine reachable from the machine on
  18. # which you want to run the socket tests, by issuing:
  19. #     tcltest remote.tcl -port 2048    # Or choose another port number.
  20. # If the machine you are running the remote server on has several IP
  21. # interfaces, you can choose which interface the server listens on for
  22. # connections by specifying the -address command line flag, so:
  23. #     tcltest remote.tcl -address your.machine.com
  24. # These options can also be set by environment variables. On Unix, you can
  25. # type these commands to the shell from which the remote server is started:
  26. #     shell% setenv serverPort 2048
  27. #     shell% setenv serverAddress your.machine.com
  28. # and subsequently you can start the remote server with:
  29. #     tcltest remote.tcl
  30. # to have it listen on port 2048 on the interface your.machine.com.
  31. #     
  32. # When the server starts, it prints out a detailed message containing its
  33. # configuration information, and it will block until killed with a Ctrl-C.
  34. # Once the remote server exists, you can run the tests in socket.test with
  35. # the server by setting two Tcl variables:
  36. #     % set remoteServerIP <name or address of machine on which server runs>
  37. #     % set remoteServerPort 2048
  38. # These variables are also settable from the environment. On Unix, you can:
  39. #     shell% setenv remoteServerIP machine.where.server.runs
  40. #     shell% senetv remoteServerPort 2048
  41. # The preamble of the socket.test file checks to see if the variables are set
  42. # either in Tcl or in the environment; if they are, it attempts to connect to
  43. # the server. If the connection is successful, the tests using the remote
  44. # server will be performed; otherwise, it will attempt to start the remote
  45. # server (via exec) on platforms that support this, on the local host,
  46. # listening at port 2048. If all fails, a message is printed and the tests
  47. # using the remote server are not performed.
  48. #
  49. # SCCS: @(#) socket.test 1.82 97/08/05 13:30:55
  50.  
  51. if {[string compare test [info procs test]] == 1} then {source defs}
  52.  
  53. if {$testConfig(socket) == 0} {
  54.     return
  55. }
  56.  
  57. #
  58. # If remoteServerIP or remoteServerPort are not set, check in the
  59. # environment variables for externally set values.
  60. #
  61.  
  62. if {![info exists remoteServerIP]} {
  63.     if {[info exists env(remoteServerIP)]} {
  64.     set remoteServerIP $env(remoteServerIP)
  65.     }
  66. }
  67. if {![info exists remoteServerPort]} {
  68.     if {[info exists env(remoteServerIP)]} {
  69.     set remoteServerPort $env(remoteServerPort)
  70.     } else {
  71.         if {[info exists remoteServerIP]} {
  72.         set remoteServerPort 2048
  73.         }
  74.     }
  75. }
  76.  
  77. #
  78. # Check if we're supposed to do tests against the remote server
  79. #
  80.  
  81. set doTestsWithRemoteServer 1
  82. if {![info exists remoteServerIP] && ($tcl_platform(platform) != "macintosh")} {
  83.     set remoteServerIP localhost
  84. }
  85. if {($doTestsWithRemoteServer == 1) && (![info exists remoteServerPort])} {
  86.     set remoteServerPort 2048
  87. }
  88.  
  89. # Attempt to connect to a remote server if one is already running. If it
  90. # is not running or for some other reason the connect fails, attempt to
  91. # start the remote server on the local host listening on port 2048. This
  92. # is only done on platforms that support exec (i.e. not on the Mac). On
  93. # platforms that do not support exec, the remote server must be started
  94. # by the user before running the tests.
  95.  
  96. set remoteProcChan ""
  97. set commandSocket ""
  98. if {$doTestsWithRemoteServer} {
  99.     catch {close $commandSocket}
  100.     if {[catch {set commandSocket [socket $remoteServerIP \
  101.                         $remoteServerPort]}] != 0} {
  102.     if {[info commands exec] == ""} {
  103.         set noRemoteTestReason "can't exec"
  104.         set doTestsWithRemoteServer 0
  105.     } elseif {$testConfig(win32s)} {
  106.         set noRemoteTestReason "\ncan't run multiple instances of tcltest under win32s."
  107.         set doTestsWithRemoteServer 0
  108.     } else {
  109.         set remoteServerIP localhost
  110.         if {[catch {set remoteProcChan \
  111.                 [open "|[list $tcltest remote.tcl \
  112.                     -serverIsSilent \
  113.                     -port $remoteServerPort \
  114.                     -address $remoteServerIP]" \
  115.                     w+]} \
  116.            msg] == 0} {
  117.         after 1000
  118.         if {[catch {set commandSocket [socket $remoteServerIP \
  119.                 $remoteServerPort]} msg] == 0} {
  120.             fconfigure $commandSocket -translation crlf -buffering line
  121.         } else {
  122.             set noRemoteTestReason $msg
  123.             set doTestsWithRemoteServer 0
  124.         }
  125.         } else {
  126.         set noRemoteTestReason "$msg $tcltest"
  127.         set doTestsWithRemoteServer 0
  128.         }
  129.     }
  130.     } else {
  131.     fconfigure $commandSocket -translation crlf -buffering line
  132.     }
  133. }
  134.  
  135. if {$doTestsWithRemoteServer == 0} {
  136.     puts "Skipping tests with remote server. See tests/socket.test for"
  137.     puts "information on how to run remote server."
  138.     if {[info exists VERBOSE] && ($VERBOSE != 0)} {
  139.     puts "Reason for not doing remote tests: $noRemoteTestReason"
  140.     }
  141. }
  142.  
  143. #
  144. # If we do the tests, define a command to send a command to the
  145. # remote server.
  146. #
  147.  
  148. if {$doTestsWithRemoteServer == 1} {
  149.     proc sendCommand {c} {
  150.     global commandSocket
  151.  
  152.     if {[eof $commandSocket]} {
  153.         error "remote server disappeared"
  154.     }
  155.  
  156.     if {[catch {puts $commandSocket $c} msg]} {
  157.         error "remote server disappaered: $msg"
  158.     }
  159.     if {[catch {puts $commandSocket "--Marker--Marker--Marker--"} msg]} {
  160.         error "remote server disappeared: $msg"
  161.     }
  162.  
  163.     set resp ""
  164.     while {1} {
  165.         set line [gets $commandSocket]
  166.         if {[eof $commandSocket]} {
  167.         error "remote server disappaered"
  168.         }
  169.         if {[string compare $line "--Marker--Marker--Marker--"] == 0} {
  170.         if {[string compare [lindex $resp 0] error] == 0} {
  171.             error [lindex $resp 1]
  172.         } else {
  173.             return [lindex $resp 1]
  174.         }
  175.         } else {
  176.         append resp $line "\n"
  177.         }
  178.     }
  179.     }
  180. }
  181.  
  182. test socket-1.1 {arg parsing for socket command} {
  183.     list [catch {socket -server} msg] $msg
  184. } {1 {no argument given for -server option}}
  185. test socket-1.2 {arg parsing for socket command} {
  186.     list [catch {socket -server foo} msg] $msg
  187. } {1 {wrong # args: should be either:
  188. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  189. socket -server command ?-myaddr addr? port}}
  190. test socket-1.3 {arg parsing for socket command} {
  191.     list [catch {socket -myaddr} msg] $msg
  192. } {1 {no argument given for -myaddr option}}
  193. test socket-1.4 {arg parsing for socket command} {
  194.     list [catch {socket -myaddr 127.0.0.1} msg] $msg
  195. } {1 {wrong # args: should be either:
  196. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  197. socket -server command ?-myaddr addr? port}}
  198. test socket-1.5 {arg parsing for socket command} {
  199.     list [catch {socket -myport} msg] $msg
  200. } {1 {no argument given for -myport option}}
  201. test socket-1.6 {arg parsing for socket command} {
  202.     list [catch {socket -myport xxxx} msg] $msg
  203. } {1 {expected integer but got "xxxx"}}
  204. test socket-1.7 {arg parsing for socket command} {
  205.     list [catch {socket -myport 2522} msg] $msg
  206. } {1 {wrong # args: should be either:
  207. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  208. socket -server command ?-myaddr addr? port}}
  209. test socket-1.8 {arg parsing for socket command} {
  210.     list [catch {socket -froboz} msg] $msg
  211. } {1 {bad option "-froboz", must be -async, -myaddr, -myport, or -server}}
  212. test socket-1.9 {arg parsing for socket command} {
  213.     list [catch {socket -server foo -myport 2521 3333} msg] $msg
  214. } {1 {Option -myport is not valid for servers}}
  215. test socket-1.10 {arg parsing for socket command} {
  216.     list [catch {socket host 2528 -junk} msg] $msg
  217. } {1 {wrong # args: should be either:
  218. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  219. socket -server command ?-myaddr addr? port}}
  220. test socket-1.11 {arg parsing for socket command} {
  221.     list [catch {socket -server callback 2520 --} msg] $msg
  222. } {1 {wrong # args: should be either:
  223. socket ?-myaddr addr? ?-myport myport? ?-async? host port
  224. socket -server command ?-myaddr addr? port}}
  225. test socket-1.12 {arg parsing for socket command} {
  226.     list [catch {socket foo badport} msg] $msg
  227. } {1 {expected integer but got "badport"}}
  228.  
  229. test socket-2.1 {tcp connection} {stdio} {
  230.     removeFile script
  231.     set f [open script w]
  232.     puts $f {
  233.     set timer [after 2000 "set x timed_out"]
  234.     set f [socket -server accept 2828]
  235.     proc accept {file addr port} {
  236.         global x
  237.         set x done
  238.             close $file
  239.     }
  240.     puts ready
  241.     vwait x
  242.     after cancel $timer
  243.     close $f
  244.     puts $x
  245.     }
  246.     close $f
  247.     set f [open "|[list $tcltest script]" r]
  248.     gets $f x
  249.     if {[catch {socket localhost 2828} msg]} {
  250.         set x $msg
  251.     } else {
  252.         lappend x [gets $f]
  253.         close $msg
  254.     }
  255.     lappend x [gets $f]
  256.     close $f
  257.     set x
  258. } {ready done {}}
  259.  
  260. if [info exists port] {
  261.     incr port
  262. } else { 
  263.     set port [expr 2048 + [pid]%1024]
  264. }
  265. test socket-2.2 {tcp connection with client port specified} {stdio} {
  266.     removeFile script
  267.     set f [open script w]
  268.     puts $f {
  269.     set timer [after 2000 "set x done"]
  270.         set f [socket -server accept 2828]
  271.     proc accept {file addr port} {
  272.             global x
  273.             puts "[gets $file] $port"
  274.             close $file
  275.             set x done
  276.     }
  277.     puts ready
  278.     vwait x
  279.     after cancel $timer
  280.     close $f
  281.     }
  282.     close $f
  283.     set f [open "|[list $tcltest script]" r]
  284.     gets $f x
  285.     global port
  286.     if {[catch {socket -myport $port localhost 2828} sock]} {
  287.         set x $sock
  288.     close [socket localhost 2828]
  289.     puts stderr $sock
  290.     } else {
  291.         puts $sock hello
  292.     flush $sock
  293.         lappend x [gets $f]
  294.         close $sock
  295.     }
  296.     close $f
  297.     set x
  298. } [list ready "hello $port"]
  299. test socket-2.3 {tcp connection with client interface specified} {stdio} {
  300.     removeFile script
  301.     set f [open script w]
  302.     puts $f {
  303.     set timer [after 2000 "set x done"]
  304.         set f [socket  -server accept 2828]
  305.     proc accept {file addr port} {
  306.             global x
  307.             puts "[gets $file] $addr"
  308.             close $file
  309.             set x done
  310.     }
  311.     puts ready
  312.     vwait x
  313.     after cancel $timer
  314.     close $f
  315.     }
  316.     close $f
  317.     set f [open "|[list $tcltest script]" r]
  318.     gets $f x
  319.     if {[catch {socket -myaddr localhost localhost 2828} sock]} {
  320.         set x $sock
  321.     } else {
  322.         puts $sock hello
  323.     flush $sock
  324.         lappend x [gets $f]
  325.         close $sock
  326.     }
  327.     close $f
  328.     set x
  329. } {ready {hello 127.0.0.1}}
  330. test socket-2.4 {tcp connection with server interface specified} {stdio} {
  331.     removeFile script
  332.     set f [open script w]
  333.     puts $f {
  334.     set timer [after 2000 "set x done"]
  335.         set f [socket -server accept -myaddr [info hostname] 2828]
  336.     proc accept {file addr port} {
  337.             global x
  338.             puts "[gets $file]"
  339.             close $file
  340.             set x done
  341.     }
  342.     puts ready
  343.     vwait x
  344.     after cancel $timer
  345.     close $f
  346.     }
  347.     close $f
  348.     set f [open "|[list $tcltest script]" r]
  349.     gets $f x
  350.     if {[catch {socket [info hostname] 2828} sock]} {
  351.         set x $sock
  352.     } else {
  353.         puts $sock hello
  354.     flush $sock
  355.         lappend x [gets $f]
  356.         close $sock
  357.     }
  358.     close $f
  359.     set x
  360. } {ready hello}
  361. test socket-2.5 {tcp connection with redundant server port} {stdio} {
  362.     removeFile script
  363.     set f [open script w]
  364.     puts $f {
  365.     set timer [after 2000 "set x done"]
  366.         set f [socket -server accept 2828]
  367.     proc accept {file addr port} {
  368.             global x
  369.             puts "[gets $file]"
  370.             close $file
  371.             set x done
  372.     }
  373.     puts ready
  374.     vwait x
  375.     after cancel $timer
  376.     close $f
  377.     }
  378.     close $f
  379.     set f [open "|[list $tcltest script]" r]
  380.     gets $f x
  381.     if {[catch {socket localhost 2828} sock]} {
  382.         set x $sock
  383.     } else {
  384.         puts $sock hello
  385.     flush $sock
  386.         lappend x [gets $f]
  387.         close $sock
  388.     }
  389.     close $f
  390.     set x
  391. } {ready hello}
  392. test socket-2.6 {tcp connection} {unixOrPc} {
  393.     set status ok
  394.     if {![catch {set sock [socket localhost 2828]}]} {
  395.     if {![catch {gets $sock}]} {
  396.         set status broken
  397.     }
  398.     close $sock
  399.     }
  400.     set status
  401. } ok
  402. test socket-2.7 {echo server, one line} {stdio} {
  403.     removeFile script
  404.     set f [open script w]
  405.     puts $f {
  406.     set timer [after 2000 "set x done"]
  407.     set f [socket -server accept 2828]
  408.     proc accept {s a p} {
  409.             fileevent $s readable [list echo $s]
  410.         fconfigure $s -translation lf -buffering line
  411.         }
  412.     proc echo {s} {
  413.          set l [gets $s]
  414.              if {[eof $s]} {
  415.                  global x
  416.                  close $s
  417.                  set x done
  418.              } else {
  419.                  puts $s $l
  420.              }
  421.     }
  422.     puts ready
  423.     vwait x
  424.     after cancel $timer
  425.     close $f
  426.     puts done
  427.     }
  428.     close $f
  429.     set f [open "|[list $tcltest script]" r]
  430.     gets $f
  431.     set s [socket localhost 2828]
  432.     fconfigure $s -buffering line -translation lf
  433.     puts $s "hello abcdefghijklmnop"
  434.     set x [gets $s]
  435.     close $s
  436.     set y [gets $f]
  437.     close $f
  438.     list $x $y
  439. } {{hello abcdefghijklmnop} done}
  440. test socket-2.8 {echo server, loop 50 times, single connection} {stdio} {
  441.     removeFile script
  442.     set f [open script w]
  443.     puts $f {
  444.     set f [socket -server accept 2828]
  445.     proc accept {s a p} {
  446.             fileevent $s readable [list echo $s]
  447.             fconfigure $s -buffering line
  448.         }
  449.     proc echo {s} {
  450.          global i
  451.              set l [gets $s]
  452.              if {[eof $s]} {
  453.                  global x
  454.                  close $s
  455.                  set x done
  456.              } else { 
  457.              incr i
  458.                  puts $s $l
  459.              }
  460.     }
  461.     set i 0
  462.     puts ready
  463.     set timer [after 20000 "set x done"]
  464.     vwait x
  465.     after cancel $timer
  466.     close $f
  467.     puts "done $i"
  468.     }
  469.     close $f
  470.     set f [open "|[list $tcltest script]" r]
  471.     gets $f
  472.     set s [socket localhost 2828]
  473.     fconfigure $s -buffering line
  474.     for {set x 0} {$x < 50} {incr x} {
  475.         puts $s "hello abcdefghijklmnop"
  476.         gets $s
  477.     }
  478.     close $s
  479.     set x [gets $f]
  480.     close $f
  481.     set x
  482. } {done 50}
  483. test socket-2.9 {socket conflict} {stdio} {
  484.     set s [socket -server accept 2828]
  485.     removeFile script
  486.     set f [open script w]
  487.     puts $f {set f [socket -server accept 2828]}
  488.     close $f
  489.     set f [open "|[list $tcltest script]" r]
  490.     gets $f
  491.     after 100
  492.     set x [list [catch {close $f} msg] $msg]
  493.     close $s
  494.     set x
  495. } {1 {couldn't open socket: address already in use
  496.     while executing
  497. "socket -server accept 2828"
  498.     (file "script" line 1)}}
  499. test socket-2.10 {close on accept, accepted socket lives} {
  500.     set done 0
  501.     set timer [after 20000 "set done timed_out"]
  502.     set ss [socket -server accept 2830]
  503.     proc accept {s a p} {
  504.     global ss
  505.     close $ss
  506.     fileevent $s readable "readit $s"
  507.     fconfigure $s -trans lf
  508.     }
  509.     proc readit {s} {
  510.     global done
  511.     gets $s
  512.     close $s
  513.     set done 1
  514.     }
  515.     set cs [socket [info hostname] 2830]
  516.     puts $cs hello
  517.     close $cs
  518.     vwait done
  519.     after cancel $timer
  520.     set done
  521. } 1
  522.  
  523. test socket-3.1 {socket conflict} {stdio} {
  524.     removeFile script
  525.     set f [open script w]
  526.     puts $f {
  527.     set f [socket -server accept 2828]
  528.     puts ready
  529.     gets stdin
  530.     close $f
  531.     }
  532.     close $f
  533.     set f [open "|[list $tcltest script]" r+]
  534.     gets $f
  535.     set x [list [catch {socket -server accept 2828} msg] \
  536.         $msg]
  537.     puts $f bye
  538.     close $f
  539.     set x
  540. } {1 {couldn't open socket: address already in use}}
  541. test socket-3.2 {server with several clients} {stdio} {
  542.     removeFile script
  543.     set f [open script w]
  544.     puts $f {
  545.     set t1 [after 30000 "set x timed_out"]
  546.     set t2 [after 31000 "set x timed_out"]
  547.     set t3 [after 32000 "set x timed_out"]
  548.     set counter 0
  549.     set s [socket -server accept 2828]
  550.     proc accept {s a p} {
  551.         fileevent $s readable [list echo $s]
  552.         fconfigure $s -buffering line
  553.     }
  554.     proc echo {s} {
  555.          global x
  556.              set l [gets $s]
  557.              if {[eof $s]} {
  558.                  close $s
  559.                  set x done
  560.              } else {
  561.                  puts $s $l
  562.              }
  563.     }
  564.     puts ready
  565.     vwait x
  566.     after cancel $t1
  567.     vwait x
  568.     after cancel $t2
  569.     vwait x
  570.     after cancel $t3
  571.     close $s
  572.     puts $x
  573.     }
  574.     close $f
  575.     set f [open "|[list $tcltest script]" r+]
  576.     set x [gets $f]
  577.     set s1 [socket localhost 2828]
  578.     fconfigure $s1 -buffering line
  579.     set s2 [socket localhost 2828]
  580.     fconfigure $s2 -buffering line
  581.     set s3 [socket localhost 2828]
  582.     fconfigure $s3 -buffering line
  583.     for {set i 0} {$i < 100} {incr i} {
  584.     puts $s1 hello,s1
  585.     gets $s1
  586.     puts $s2 hello,s2
  587.     gets $s2
  588.     puts $s3 hello,s3
  589.     gets $s3
  590.     }
  591.     close $s1
  592.     close $s2
  593.     close $s3
  594.     lappend x [gets $f]
  595.     close $f
  596.     set x
  597. } {ready done}
  598.  
  599. test socket-4.1 {server with several clients} {stdio} {
  600.     removeFile script
  601.     set f [open script w]
  602.     puts $f {
  603.     gets stdin
  604.     set s [socket localhost 2828]
  605.     fconfigure $s -buffering line
  606.     for {set i 0} {$i < 100} {incr i} {
  607.         puts $s hello
  608.         gets $s
  609.     }
  610.     close $s
  611.     puts bye
  612.     gets stdin
  613.     }
  614.     close $f
  615.     set p1 [open "|[list $tcltest script]" r+]
  616.     fconfigure $p1 -buffering line
  617.     set p2 [open "|[list $tcltest script]" r+]
  618.     fconfigure $p2 -buffering line
  619.     set p3 [open "|[list $tcltest script]" r+]
  620.     fconfigure $p3 -buffering line
  621.     proc accept {s a p} {
  622.     fconfigure $s -buffering line
  623.     fileevent $s readable [list echo $s]
  624.     }
  625.     proc echo {s} {
  626.     global x
  627.         set l [gets $s]
  628.         if {[eof $s]} {
  629.             close $s
  630.             set x done
  631.         } else {
  632.             puts $s $l
  633.         }
  634.     }
  635.     set t1 [after 30000 "set x timed_out"]
  636.     set t2 [after 31000 "set x timed_out"]
  637.     set t3 [after 32000 "set x timed_out"]
  638.     set s [socket -server accept 2828]
  639.     puts $p1 open
  640.     puts $p2 open
  641.     puts $p3 open
  642.     vwait x
  643.     vwait x
  644.     vwait x
  645.     after cancel $t1
  646.     after cancel $t2
  647.     after cancel $t3
  648.     close $s
  649.     set l ""
  650.     lappend l [list p1 [gets $p1] $x]
  651.     lappend l [list p2 [gets $p2] $x]
  652.     lappend l [list p3 [gets $p3] $x]
  653.     puts $p1 bye
  654.     puts $p2 bye
  655.     puts $p3 bye
  656.     close $p1
  657.     close $p2
  658.     close $p3
  659.     set l
  660. } {{p1 bye done} {p2 bye done} {p3 bye done}}
  661. test socket-4.2 {byte order problems, socket numbers, htons} {
  662.     set x ok
  663.     if {[catch {socket -server dodo 0x3000} msg]} {
  664.     set x $msg
  665.     } else {
  666.     close $msg
  667.     }
  668.     set x
  669. } ok
  670.  
  671. test socket-5.1 {byte order problems, socket numbers, htons} {unixOnly} {
  672.     #
  673.     # THIS TEST WILL FAIL if you are running as superuser.
  674.     #
  675.     set x {couldn't open socket: not owner}
  676.     if {![catch {socket -server dodo 0x1} msg]} {
  677.         set x {htons problem, should be disallowed, are you running as SU?}
  678.     close $msg
  679.     }
  680.     set x
  681. } {couldn't open socket: not owner}
  682. test socket-5.2 {byte order problems, socket numbers, htons} {
  683.     set x {couldn't open socket: port number too high}
  684.     if {![catch {socket -server dodo 0x10000} msg]} {
  685.     set x {port resolution problem, should be disallowed}
  686.     close $msg
  687.     }
  688.     set x
  689. } {couldn't open socket: port number too high}
  690. test socket-5.3 {byte order problems, socket numbers, htons} {unixOnly} {
  691.     #
  692.     # THIS TEST WILL FAIL if you are running as superuser.
  693.     #
  694.     set x {couldn't open socket: not owner}
  695.     if {![catch {socket -server dodo 21} msg]} {
  696.     set x {htons problem, should be disallowed, are you running as SU?}
  697.     close $msg
  698.     }
  699.     set x
  700. } {couldn't open socket: not owner}
  701.  
  702. test socket-6.1 {accept callback error} {stdio} {
  703.     removeFile script
  704.     set f [open script w]
  705.     puts $f {
  706.     gets stdin
  707.     socket localhost 2848
  708.     }
  709.     close $f
  710.     set f [open "|[list $tcltest script]" r+]
  711.     proc bgerror args {
  712.     global x
  713.     set x $args
  714.     }
  715.     proc accept {s a p} {expr 10 / 0}
  716.     set s [socket -server accept 2848]
  717.     puts $f hello
  718.     close $f
  719.     set timer [after 10000 "set x timed_out"]
  720.     vwait x
  721.     after cancel $timer
  722.     close $s
  723.     rename bgerror {}
  724.     set x
  725. } {{divide by zero}}
  726.  
  727. test socket-7.1 {testing socket specific options} {stdio} {
  728.     removeFile script
  729.     set f [open script w]
  730.     puts $f {
  731.     socket -server accept 2820
  732.     proc accept args {
  733.         global x
  734.         set x done
  735.     }
  736.     puts ready
  737.     set timer [after 10000 "set x timed_out"]
  738.     vwait x
  739.     after cancel $timer
  740.     }
  741.     close $f
  742.     set f [open "|[list $tcltest script]" r]
  743.     gets $f
  744.     set s [socket localhost 2820]
  745.     set p [fconfigure $s -peername]
  746.     close $s
  747.     close $f
  748.     set l ""
  749.     lappend l [string compare [lindex $p 0] 127.0.0.1]
  750.     lappend l [string compare [lindex $p 2] 2820]
  751.     lappend l [llength $p]
  752. } {0 0 3}
  753. test socket-7.2 {testing socket specific options} {stdio} {
  754.     removeFile script
  755.     set f [open script w]
  756.     puts $f {
  757.     socket -server accept 2821
  758.     proc accept args {
  759.         global x
  760.         set x done
  761.     }
  762.     puts ready
  763.     set timer [after 10000 "set x timed_out"]
  764.     vwait x
  765.     after cancel $timer
  766.     }
  767.     close $f
  768.     set f [open "|[list $tcltest script]" r]
  769.     gets $f
  770.     set s [socket localhost 2821]
  771.     set p [fconfigure $s -sockname]
  772.     close $s
  773.     close $f
  774.     set l ""
  775.     lappend l [llength $p]
  776.     lappend l [lindex $p 0]
  777.     lappend l [expr [lindex $p 2] == 2821]
  778. } {3 127.0.0.1 0}
  779. test socket-7.3 {testing socket specific options} {
  780.     set s [socket -server accept 2822]
  781.     set l [fconfigure $s]
  782.     close $s
  783.     update
  784.     llength $l
  785. } 10
  786. test socket-7.4 {testing socket specific options} {
  787.     set s [socket -server accept 2823]
  788.     proc accept {s a p} {
  789.     global x
  790.     set x [fconfigure $s -sockname]
  791.     close $s
  792.     }
  793.     set s1 [socket [info hostname] 2823]
  794.     set timer [after 10000 "set x timed_out"]
  795.     vwait x
  796.     after cancel $timer
  797.     close $s
  798.     close $s1
  799.     set l ""
  800.     lappend l [lindex $x 2] [llength $x]
  801. } {2823 3}
  802. test socket-7.5 {testing socket specific options} {unixOrPc} {
  803.     set s [socket -server accept 2829]
  804.     proc accept {s a p} {
  805.     global x
  806.     set x [fconfigure $s -sockname]
  807.     close $s
  808.     }
  809.     set s1 [socket localhost 2829]
  810.     set timer [after 10000 "set x timed_out"]
  811.     vwait x
  812.     after cancel $timer
  813.     close $s
  814.     close $s1
  815.     set l ""
  816.     lappend l [lindex $x 0] [lindex $x 2] [llength $x]
  817. } {127.0.0.1 2829 3}
  818.  
  819. test socket-8.1 {testing -async flag on sockets} {
  820.     # NOTE: This test may fail on some Solaris 2.4 systems. If it does,
  821.     # check that you have these patches installed (using showrev -p):
  822.     #
  823.     # 101907-05, 101925-02, 101945-14, 101959-03, 101969-05, 101973-03,
  824.     # 101977-03, 101981-02, 101985-01, 102001-03, 102003-01, 102007-01,
  825.     # 102011-02, 102024-01, 102039-01, 102044-01, 102048-01, 102062-03,
  826.     # 102066-04, 102070-01, 102105-01, 102153-03, 102216-01, 102232-01,
  827.     # 101878-03, 101879-01, 101880-03, 101933-01, 101950-01, 102030-01,
  828.     # 102057-08, 102140-01, 101920-02, 101921-09, 101922-07, 101923-03
  829.     #
  830.     # If after installing these patches you are still experiencing a
  831.     # problem, please email jyl@eng.sun.com. We have not observed this
  832.     # failure on Solaris 2.5, so another option (instead of installing
  833.     # these patches) is to upgrade to Solaris 2.5.
  834.     set s [socket -server accept 2830]
  835.     proc accept {s a p} {
  836.     global x
  837.     puts $s bye
  838.     close $s
  839.     set x done
  840.     }
  841.     set s1 [socket -async [info hostname] 2830]
  842.     vwait x
  843.     set z [gets $s1]
  844.     close $s
  845.     close $s1
  846.     set z
  847. } bye
  848.  
  849. test socket-9.1 {testing spurious events} {
  850.     set len 0
  851.     set spurious 0
  852.     set done 0
  853.     proc readlittle {s} {
  854.     global spurious done len
  855.     set l [read $s 1]
  856.     if {[string length $l] == 0} {
  857.         if {![eof $s]} {
  858.         incr spurious
  859.         } else {
  860.         close $s
  861.         set done 1
  862.         }
  863.     } else {
  864.         incr len [string length $l]
  865.     }
  866.     }
  867.     proc accept {s a p} {
  868.     fconfigure $s -buffering none -blocking off
  869.     fileevent $s readable [list readlittle $s]
  870.     }
  871.     set s [socket -server accept 2831]
  872.     set c [socket [info hostname] 2831]
  873.     puts -nonewline $c 01234567890123456789012345678901234567890123456789
  874.     close $c
  875.     set timer [after 10000 "set done timed_out"]
  876.     vwait done
  877.     after cancel $timer
  878.     close $s
  879.     list $spurious $len
  880. } {0 50}
  881. test socket-9.2 {testing async write, fileevents, flush on close} {tempNotMac} {
  882.     set firstblock ""
  883.     for {set i 0} {$i < 5} {incr i} {set firstblock "a$firstblock$firstblock"}
  884.     set secondblock ""
  885.     for {set i 0} {$i < 16} {incr i} {
  886.     set secondblock "b$secondblock$secondblock"
  887.     }
  888.     set l [socket -server accept 2832]
  889.     proc accept {s a p} {
  890.     fconfigure $s -blocking 0 -translation lf -buffersize 16384 \
  891.         -buffering line
  892.     fileevent $s readable "readable $s"
  893.     }
  894.     proc readable {s} {
  895.     set l [gets $s]
  896.     fileevent $s readable {}
  897.     after 1000 respond $s
  898.     }
  899.     proc respond {s} {
  900.     global firstblock
  901.     puts -nonewline $s $firstblock
  902.     after 1000 writedata $s
  903.     }
  904.     proc writedata {s} {
  905.     global secondblock
  906.     puts -nonewline $s $secondblock
  907.     close $s
  908.     }
  909.     set s [socket [info hostname] 2832]
  910.     fconfigure $s -blocking 0 -trans lf -buffering line
  911.     set count 0
  912.     puts $s hello
  913.     proc readit {s} {
  914.     global count done
  915.     set l [read $s]
  916.     incr count [string length $l]
  917.     if {[eof $s]} {
  918.         close $s
  919.         set done 1
  920.     }
  921.     }
  922.     fileevent $s readable "readit $s"
  923.     set timer [after 10000 "set done timed_out"]
  924.     vwait done
  925.     after cancel $timer
  926.     close $l
  927.     set count
  928. } 65566
  929. test socket-9.3 {testing EOF stickyness} {
  930.     proc count_to_eof {s} {
  931.     global count done timer
  932.     set l [gets $s]
  933.     if {[eof $s]} {
  934.         incr count
  935.         if {$count > 9} {
  936.         close $s
  937.         set done true
  938.         set count {eof is sticky}
  939.         after cancel $timer
  940.         }
  941.     }
  942.     }
  943.     proc timerproc {} {
  944.     global done count c
  945.     set done true
  946.     set count {timer went off, eof is not sticky}
  947.     close $c
  948.     }    
  949.     set count 0
  950.     set done false
  951.     proc write_then_close {s} {
  952.     puts $s bye
  953.     close $s
  954.     }
  955.     proc accept {s a p} {
  956.     fconfigure $s -buffering line -translation lf
  957.     fileevent $s writable "write_then_close $s"
  958.     }
  959.     set s [socket -server accept 2833]
  960.     set c [socket [info hostname] 2833]
  961.     fconfigure $c -blocking off -buffering line -translation lf
  962.     fileevent $c readable "count_to_eof $c"
  963.     set timer [after 1000 timerproc]
  964.     vwait done
  965.     close $s
  966.     set count
  967. } {eof is sticky}
  968.  
  969. removeFile script
  970.  
  971. #
  972. # The rest of the tests are run only if we are doing testing against
  973. # a remote server.
  974. #
  975.  
  976. if {$doTestsWithRemoteServer == 0} {
  977.     return
  978. }
  979.  
  980. test socket-10.1 {tcp connection} {
  981.     sendCommand {
  982.     set socket9_1_test_server [socket -server accept 2834]
  983.     proc accept {s a p} {
  984.         puts $s done
  985.         close $s
  986.     }
  987.     }
  988.     set s [socket $remoteServerIP 2834]
  989.     set r [gets $s]
  990.     close $s
  991.     sendCommand {close $socket9_1_test_server}
  992.     set r
  993. } done
  994. test socket-10.2 {client specifies its port} {
  995.     if {[info exists port]} {
  996.     incr port
  997.     } else {
  998.     set port [expr 2048 + [pid]%1024]
  999.     }
  1000.     sendCommand {
  1001.     set socket9_2_test_server [socket -server accept 2835]
  1002.     proc accept {s a p} {
  1003.         puts $s $p
  1004.         close $s
  1005.     }
  1006.     }
  1007.     set s [socket -myport $port $remoteServerIP 2835]
  1008.     set r [gets $s]
  1009.     close $s
  1010.     sendCommand {close $socket9_2_test_server}
  1011.     if {$r == $port} {
  1012.     set result ok
  1013.     } else {
  1014.     set result broken
  1015.     }
  1016.     set result
  1017. } ok
  1018. #
  1019. # Tests io-10.3, io-10.4 have been removed.
  1020. #
  1021. test socket-10.3 {trying to connect, no server} {
  1022.     set status ok
  1023.     if {![catch {set s [socket $remoteServerIp 2836]}]} {
  1024.     if {![catch {gets $s}]} {
  1025.         set status broken
  1026.     }
  1027.     close $s
  1028.     }
  1029.     set status
  1030. } ok
  1031. test socket-10.4 {remote echo, one line} {
  1032.     sendCommand {
  1033.     set socket10_6_test_server [socket -server accept 2836]
  1034.     proc accept {s a p} {
  1035.         fileevent $s readable [list echo $s]
  1036.         fconfigure $s -buffering line -translation crlf
  1037.     }
  1038.     proc echo {s} {
  1039.         set l [gets $s]
  1040.         if {[eof $s]} {
  1041.         close $s
  1042.         } else {
  1043.         puts $s $l
  1044.         }
  1045.     }
  1046.     }
  1047.     set f [socket $remoteServerIP 2836]
  1048.     fconfigure $f -translation crlf -buffering line
  1049.     puts $f hello
  1050.     set r [gets $f]
  1051.     close $f
  1052.     sendCommand {close $socket10_6_test_server}
  1053.     set r
  1054. } hello
  1055. test socket-10.5 {remote echo, 50 lines} {
  1056.     sendCommand {
  1057.     set socket10_7_test_server [socket -server accept 2836]
  1058.     proc accept {s a p} {
  1059.         fileevent $s readable [list echo $s]
  1060.         fconfigure $s -buffering line -translation crlf
  1061.     }
  1062.     proc echo {s} {
  1063.         set l [gets $s]
  1064.         if {[eof $s]} {
  1065.         close $s
  1066.         } else {
  1067.         puts $s $l
  1068.         }
  1069.     }
  1070.     }
  1071.     set f [socket $remoteServerIP 2836]
  1072.     fconfigure $f -translation crlf -buffering line
  1073.     for {set cnt 0} {$cnt < 50} {incr cnt} {
  1074.     puts $f "hello, $cnt"
  1075.     if {[string compare [gets $f] "hello, $cnt"] != 0} {
  1076.         break
  1077.     }
  1078.     }
  1079.     close $f
  1080.     sendCommand {close $socket10_7_test_server}
  1081.     set cnt
  1082. } 50
  1083. # Macintosh sockets can have more than one server per port
  1084. if {$tcl_platform(platform) == "macintosh"} {
  1085.     set conflictResult {0 2836}
  1086. } else {
  1087.     set conflictResult {1 {couldn't open socket: address already in use}}
  1088. }
  1089. test socket-10.6 {socket conflict} {
  1090.     set s1 [socket -server accept 2836]
  1091.     if {[catch {set s2 [socket -server accept 2836]} msg]} {
  1092.     set result [list 1 $msg]
  1093.     } else {
  1094.     set result [list 0 [lindex [fconfigure $s2 -sockname] 2]]
  1095.     close $s2
  1096.     }
  1097.     close $s1
  1098.     set result
  1099. } $conflictResult
  1100. test socket-10.7 {server with several clients} {
  1101.     sendCommand {
  1102.     set socket10_9_test_server [socket -server accept 2836]
  1103.     proc accept {s a p} {
  1104.         fconfigure $s -buffering line
  1105.         fileevent $s readable [list echo $s]
  1106.     }
  1107.     proc echo {s} {
  1108.         set l [gets $s]
  1109.         if {[eof $s]} {
  1110.         close $s
  1111.         } else {
  1112.         puts $s $l
  1113.         }
  1114.     }
  1115.     }
  1116.     set s1 [socket $remoteServerIP 2836]
  1117.     fconfigure $s1 -buffering line
  1118.     set s2 [socket $remoteServerIP 2836]
  1119.     fconfigure $s2 -buffering line
  1120.     set s3 [socket $remoteServerIP 2836]
  1121.     fconfigure $s3 -buffering line
  1122.     for {set i 0} {$i < 100} {incr i} {
  1123.     puts $s1 hello,s1
  1124.     gets $s1
  1125.     puts $s2 hello,s2
  1126.     gets $s2
  1127.     puts $s3 hello,s3
  1128.     gets $s3
  1129.     }
  1130.     close $s1
  1131.     close $s2
  1132.     close $s3
  1133.     sendCommand {close $socket10_9_test_server}
  1134.     set i
  1135. } 100    
  1136. test socket-10.8 {client with several servers} {
  1137.     sendCommand {
  1138.     set s1 [socket -server "accept 4003" 4003]
  1139.     set s2 [socket -server "accept 4004" 4004]
  1140.     set s3 [socket -server "accept 4005" 4005]
  1141.     proc accept {mp s a p} {
  1142.         puts $s $mp
  1143.         close $s
  1144.     }
  1145.     }
  1146.     set s1 [socket $remoteServerIP 4003]
  1147.     set s2 [socket $remoteServerIP 4004]
  1148.     set s3 [socket $remoteServerIP 4005]
  1149.     set l ""
  1150.     lappend l [gets $s1] [gets $s1] [eof $s1] [gets $s2] [gets $s2] [eof $s2] \
  1151.     [gets $s3] [gets $s3] [eof $s3]
  1152.     close $s1
  1153.     close $s2
  1154.     close $s3
  1155.     sendCommand {
  1156.     close $s1
  1157.     close $s2
  1158.     close $s3
  1159.     }
  1160.     set l
  1161. } {4003 {} 1 4004 {} 1 4005 {} 1}
  1162. test socket-10.9 {accept callback error} {
  1163.     set s [socket -server accept 2836]
  1164.     proc accept {s a p} {expr 10 / 0}
  1165.     proc bgerror args {
  1166.     global x
  1167.     set x $args
  1168.     }
  1169.     if {[catch {sendCommand {
  1170.         set peername [fconfigure $callerSocket -peername]
  1171.         set s [socket [lindex $peername 0] 2836]
  1172.         close $s
  1173.          }} msg]} {
  1174.     close $s
  1175.     error $msg
  1176.     }
  1177.     set timer [after 10000 "set x timed_out"]
  1178.     vwait x
  1179.     after cancel $timer
  1180.     close $s
  1181.     rename bgerror {}
  1182.     set x
  1183. } {{divide by zero}}
  1184. test socket-10.10 {testing socket specific options} {
  1185.     sendCommand {
  1186.     set socket10_12_test_server [socket -server accept 2836]
  1187.     proc accept {s a p} {close $s}
  1188.     }
  1189.     set s [socket $remoteServerIP 2836]
  1190.     set p [fconfigure $s -peername]
  1191.     set n [fconfigure $s -sockname]
  1192.     set l ""
  1193.     lappend l [lindex $p 2] [llength $p] [llength $p]
  1194.     close $s
  1195.     sendCommand {close $socket10_12_test_server}
  1196.     set l
  1197. } {2836 3 3}
  1198. test socket-10.11 {testing spurious events} {
  1199.     sendCommand {
  1200.     set socket10_13_test_server [socket -server accept 2836]
  1201.     proc accept {s a p} {
  1202.         fconfigure $s -translation "auto lf"
  1203.         after 100 writesome $s
  1204.     }
  1205.     proc writesome {s} {
  1206.         for {set i 0} {$i < 100} {incr i} {
  1207.         puts $s "line $i from remote server"
  1208.         }
  1209.         close $s
  1210.     }
  1211.     }
  1212.     set len 0
  1213.     set spurious 0
  1214.     set done 0
  1215.     proc readlittle {s} {
  1216.     global spurious done len
  1217.     set l [read $s 1]
  1218.     if {[string length $l] == 0} {
  1219.         if {![eof $s]} {
  1220.         incr spurious
  1221.         } else {
  1222.         close $s
  1223.         set done 1
  1224.         }
  1225.     } else {
  1226.         incr len [string length $l]
  1227.     }
  1228.     }
  1229.     set c [socket $remoteServerIP 2836]
  1230.     fileevent $c readable "readlittle $c"
  1231.     set timer [after 10000 "set done timed_out"]
  1232.     vwait done
  1233.     after cancel $timer
  1234.     sendCommand {close $socket10_13_test_server}
  1235.     list $spurious $len
  1236. } {0 2690}
  1237. test socket-10.12 {testing EOF stickyness} {
  1238.     set counter 0
  1239.     set done 0
  1240.     proc count_up {s} {
  1241.     global counter done after_id
  1242.     set l [gets $s]
  1243.     if {[eof $s]} {
  1244.         incr counter
  1245.         if {$counter > 9} {
  1246.         set done {EOF is sticky}
  1247.         after cancel $after_id
  1248.         close $s
  1249.         }
  1250.     }
  1251.     }
  1252.     proc timed_out {} {
  1253.     global c done
  1254.     set done {timed_out, EOF is not sticky}
  1255.     close $c
  1256.     }
  1257.     sendCommand {
  1258.     set socket10_14_test_server [socket -server accept 2836]
  1259.     proc accept {s a p} {
  1260.         after 100 close $s
  1261.     }
  1262.     }
  1263.     set c [socket $remoteServerIP 2836]
  1264.     fileevent $c readable "count_up $c"
  1265.     set after_id [after 1000 timed_out]
  1266.     vwait done
  1267.     sendCommand {close $socket10_14_test_server}
  1268.     set done
  1269. } {EOF is sticky}
  1270. test socket-10.13 {testing async write, async flush, async close} {
  1271.     proc readit {s} {
  1272.     global count done
  1273.     set l [read $s]
  1274.     incr count [string length $l]
  1275.     if {[eof $s]} {
  1276.         close $s
  1277.         set done 1
  1278.     }
  1279.     }
  1280.     sendCommand {
  1281.     set firstblock ""
  1282.     for {set i 0} {$i < 5} {incr i} {
  1283.         set firstblock "a$firstblock$firstblock"
  1284.     }
  1285.     set secondblock ""
  1286.     for {set i 0} {$i < 16} {incr i} {
  1287.         set secondblock "b$secondblock$secondblock"
  1288.     }
  1289.     set l [socket -server accept 2845]
  1290.     proc accept {s a p} {
  1291.         fconfigure $s -blocking 0 -translation lf -buffersize 16384 \
  1292.         -buffering line
  1293.         fileevent $s readable "readable $s"
  1294.     }
  1295.     proc readable {s} {
  1296.         set l [gets $s]
  1297.         fileevent $s readable {}
  1298.         after 1000 respond $s
  1299.     }
  1300.     proc respond {s} {
  1301.         global firstblock
  1302.         puts -nonewline $s $firstblock
  1303.         after 1000 writedata $s
  1304.     }
  1305.     proc writedata {s} {
  1306.         global secondblock
  1307.         puts -nonewline $s $secondblock
  1308.         close $s
  1309.     }
  1310.     }
  1311.     set s [socket $remoteServerIP 2845]
  1312.     fconfigure $s -blocking 0 -trans lf -buffering line
  1313.     set count 0
  1314.     puts $s hello
  1315.     fileevent $s readable "readit $s"
  1316.     set timer [after 10000 "set done timed_out"]
  1317.     vwait done
  1318.     after cancel $timer
  1319.     sendCommand {close $l}
  1320.     set count
  1321. } 65566
  1322.  
  1323. if {[string match sock* $commandSocket] == 1} {
  1324.    puts $commandSocket exit
  1325.    flush $commandSocket
  1326. }
  1327. catch {close $commandSocket}
  1328. catch {close $remoteProcChan}
  1329.  
  1330. set x ""
  1331. unset x
  1332.